package com.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxt.sys.constast.SYSConstast;
import com.sxt.sys.domain.Menu;
import com.sxt.sys.domain.User;
import com.sxt.sys.service.MenuService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.TreeNode;
import com.sxt.sys.utils.TreeNodeBuilder;
import com.sxt.sys.utils.WebUtils;
import com.sxt.sys.vo.MenuVo;

/**
 * 权限管理的控制器
 * @author LJH
 *
 */
@Controller
@RequestMapping("menu")
public class MenuController {

	@Autowired
	private MenuService menuService;
	
	/**
	 * 加载主页右边的导航树
	 */
	@RequestMapping("loadIndexLeftMenuTree")
	@ResponseBody
	public List<TreeNode> loadIndexLeftMenuTree(MenuVo menuVo){
		List<Menu> menus=null;
		User user=WebUtils.getCurrentUser();
		if(user.getType()==SYSConstast.USER_TYPE_SUPER){
			menus=this.menuService.queryAllMenusForList(menuVo);
		}else{
			menus=this.menuService.queryMenusByUserIdForList(menuVo, user.getUserid());
		}
		List<TreeNode> treeNodes=new ArrayList<>();
		if(null!=menus&&menus.size()>0){
			for (Menu menu : menus) {
				Integer id=menu.getId();
				Integer pid=menu.getPid();
				String name=menu.getName();
				Boolean isParent=menu.getParent()==SYSConstast.MENU_PARENT_TRUE?true:false;
				Boolean open=menu.getOpen()==SYSConstast.MENU_OPEN_TRUE?true:false;
				String href=menu.getHref();
				String icon=menu.getIcon();
				String tabIcon=menu.getTabicon();
				treeNodes.add(new TreeNode(id, pid, name, isParent, open, href, icon, tabIcon));
			}
		}
		return treeNodes;
	}
	
	
	/**
	 * 跳转到菜单管理的页面
	 */
	@RequestMapping("toMenuManager")
	public String toMenuManager(){
		return "system/menuManager";
	}
	
	/**
	 * 加载菜单管理页面的树列表
	 */
	@RequestMapping("loadMenuTree")
	@ResponseBody
	public List<TreeNode> loadMenuTree(MenuVo menuVo){
		List<Menu> menus=this.menuService.queryAllMenusForList(menuVo);
		List<TreeNode> treeNodes=new ArrayList<>();
		if(null!=menus&&menus.size()>0){
			for (Menu menu : menus) {
				Integer id=menu.getId();
				Integer pid=menu.getPid();
				String name=menu.getName();
				Boolean isParent=menu.getParent()==SYSConstast.MENU_PARENT_TRUE?true:false;
				Boolean open=menu.getOpen()==SYSConstast.MENU_OPEN_TRUE?true:false;
				String href=menu.getHref();
				String icon=menu.getIcon();
				String tabIcon=menu.getTabicon();
				treeNodes.add(new TreeNode(id, pid, name, isParent, open, href, icon, tabIcon));
			}
		}
		return treeNodes;
	}
	
	/**
	 * 查询所有菜单返回DataGridView
	 */
	@RequestMapping("loadAllMenu")
	@ResponseBody
	public DataGridView loadAllMenu(MenuVo menuVo){
		return this.menuService.queryAllMenu(menuVo);
	}
	
	
	/**
	 * 加载符合easyui combotree的json数据
	 */
	@RequestMapping("loadMenuTreeForCombotree")
	@ResponseBody
	public List<TreeNode> loadMenuTreeForCombotree(MenuVo menuVo){
		//1,查询出所有可用的菜单
		List<Menu> menus=this.menuService.queryAllMenusForList(menuVo);
		//2,根据List<TreeNode>
		List<TreeNode> treeNodes=new ArrayList<>();
		if(null!=menus&&menus.size()>0){
			for (Menu m : menus) {
				treeNodes.add(new TreeNode(m.getId(), m.getPid(), m.getName()));
			}
		}
		Integer topRootPid=0;
		List<TreeNode> nodes=TreeNodeBuilder.builder(treeNodes,topRootPid);
		return nodes;
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("addMenu")
	@ResponseBody
	public Map<String,Object> addMenu(MenuVo menuVo){
		Map<String,Object> map=new HashMap<>();
		try {
			this.menuService.addMenu(menuVo);
			map.put("msg", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "保存失败");
		}
		return map;
	}
	/**
	 * 更新
	 */
	@RequestMapping("updateMenu")
	@ResponseBody
	public Map<String,Object> updateMenu(MenuVo menuVo){
		Map<String,Object> map=new HashMap<>();
		try {
			this.menuService.updateMenu(menuVo);
			map.put("msg", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "修改失败");
		}
		return map;
	}
	
	/**
	 * 根据菜单ID查询子节点的个数
	 */
	@RequestMapping("queryChildrenNodeById")
	@ResponseBody
	public Integer queryChildrenNodeById(MenuVo menuVo){
		Integer count=this.menuService.queryChildrenNodeById(menuVo.getId());
		return count;
	}
	/**
	 * 删除
	 */
	@RequestMapping("deleteMenu")
	@ResponseBody
	public Map<String,Object> deleteMenu(MenuVo menuVo){
		Map<String,Object> map=new HashMap<>();
		try {
			this.menuService.deleteMenu(menuVo);
			map.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "删除失败");
		}
		return map;
	}
}
